package com.atguigu.tingshu.album.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.common.cache.DataCache;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;

    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;

    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;

    @Autowired
    private BaseCategoryViewMapper baseCategoryViewMapper;


    @Override
    @DataCache(prefix = "album:category:all")
    public List<JSONObject> getCategoryList() {
        //1. 创建响应结果集,用于接收所有一级分类数据
        List<JSONObject> resultList = new ArrayList<>();
        //2. 查询所有分类数据(查询视图)
        List<BaseCategoryView> categoryViewList = baseCategoryViewMapper.selectList(null);

        //3. 数据结果处理
        Map<Long, List<BaseCategoryView>> category1Map = categoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
        //3.1. 对所有数据结果按一级分类ID进行分组处理,获得一级分类Map
        for (Map.Entry<Long, List<BaseCategoryView>> entry1 : category1Map.entrySet()) {
            //3.2. 打包一级分类数据
            JSONObject categoryInfo1 = new JSONObject();
            //3.2.1. 封装一级分类ID
            categoryInfo1.put("categoryId", entry1.getKey());
            //3.2.2. 封装一级分类名称
            categoryInfo1.put("categoryName", entry1.getValue().get(0).getCategory1Name());

            //4. 二级分类数据处理
            List<JSONObject> categoryList2 = new ArrayList<>();
            //4.1 对一级分类集合按照二级ID进行分组处理
            Map<Long, List<BaseCategoryView>> category2Map = entry1.getValue().stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            for (Map.Entry<Long, List<BaseCategoryView>> entry2 : category2Map.entrySet()) {
                //4.2 打包二类分类数据信息
                JSONObject categoryInfo2 = new JSONObject();
                //4.2.1 封装二类分类数据ID
                categoryInfo2.put("categoryId", entry2.getKey());
                //4.2.2 封装二类分类数据名称
                categoryInfo2.put("categoryName", entry2.getValue().get(0).getCategory2Name());
                categoryList2.add(categoryInfo2);

                //5. 三级分类数据处理
                List<JSONObject> categoryList3 = new ArrayList<>();
                //5.1 对二级分类集合按三级分类ID分组
                for (BaseCategoryView category3Map : entry2.getValue()) {
                    //5.2 打包三级分类数据信息
                    JSONObject categoryInfo3 = new JSONObject();
                    //5.2.1 封装三级分类数据ID
                    categoryInfo3.put("categoryId", category3Map.getCategory3Id());
                    //5.2.2 封装三级分类数据名称
                    categoryInfo3.put("categoryName", category3Map.getCategory3Name());
                    categoryList3.add(categoryInfo3);
                }
                //5.3 将三级分类集合封装至二级分类的categoryChild属性中
                categoryInfo2.put("categoryChild", categoryList3);
            }
            //4.3 将耳机分类集合封装至一类集合的categoryChild属性中
            categoryInfo1.put("categoryChild", categoryList2);
            resultList.add(categoryInfo1);
        }
        return resultList;
    }

    @Autowired
    private BaseAttributeMapper baseAttributeMapper;

    @Override
    @DataCache(prefix = "album:category:attribute:")
    public List<BaseAttribute> getAttributesById(Long category1Id) {
        List<BaseAttribute> attributeList = baseAttributeMapper.getAttributesById(category1Id);
        return attributeList;
    }

    /**
     * 根据三级分类ID获取专辑分类信息
     *
     * @param category3Id 专辑三级分类id
     * @return baseCategoryView 专辑分类信息
     */
    @Override
    @DataCache(prefix="alblum:categoryview:",ttl=3600, timeUnit= TimeUnit.SECONDS)
    public BaseCategoryView getCategoryView(Long category3Id) {
        return baseCategoryViewMapper.selectById(category3Id);
    }

    /**
     * 根据一级分类ID查询三级分类TOP7集合
     *
     * @param category1Id 级分类ID
     * @return List<JSONObject> 三级分类TOP7集合
     */
    @Override
    @DataCache(prefix = "album:category:top3:")
    public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {
        //根据一级分类ID获取二级分类ID集合
        List<BaseCategory2> baseCategory2List = baseCategory2Mapper.selectList(
                new LambdaQueryWrapper<BaseCategory2>()
                        .eq(BaseCategory2::getCategory1Id, category1Id)
                        .select(BaseCategory2::getId)
        );

        if (CollUtil.isNotEmpty(baseCategory2List)) {
            List<Long> baseCategory2IdList = baseCategory2List
                    .stream().map(BaseCategory2::getId).collect(Collectors.toList());
            //根据二级分类idList获取三级分类集合
            List<BaseCategory3> baseCategory3List = baseCategory3Mapper.selectList(
                    new LambdaQueryWrapper<BaseCategory3>()
                    //三级分类范围
                    .in(BaseCategory3::getCategory2Id, baseCategory2IdList)
                    //三级置顶分类
                    .eq(BaseCategory3::getIsTop, 1)
                    //三级分类排序
                    .orderByAsc(BaseCategory3::getOrderNum)
                    .last("LIMIT 7")
            );
            System.err.println(baseCategory3List);
            return baseCategory3List;
        }
        return null;
    }

    /**
     * 获取一级分类全部分类信息
     *
     * @param category1Id 一级分类ID
     * @return JSONObject
     */
    @Override
    @DataCache(prefix = "album:category:list")
    public JSONObject getBaseCategoryList(Long category1Id) {
        List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(
                new LambdaQueryWrapper<BaseCategoryView>()
                        .eq(BaseCategoryView::getCategory1Id, category1Id));
        if (CollUtil.isNotEmpty(baseCategoryViewList)) {
            BaseCategoryView baseCategoryView = baseCategoryViewList.get(0);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("categoryId", category1Id);
            jsonObject.put("categoryName", baseCategoryView.getCategory1Name());

            ArrayList<JSONObject> jsonObject2List = new ArrayList<>();
            Map<Long, List<BaseCategoryView>> map2 = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            for (Map.Entry<Long, List<BaseCategoryView>> entry2 : map2.entrySet()) {
                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.put("categoryId",entry2.getValue().get(0).getCategory2Id());
                jsonObject2.put("categoryName",entry2.getValue().get(0).getCategory2Name());
                jsonObject2List.add(jsonObject2);

                ArrayList<JSONObject> jsonObject3List = new ArrayList<>();
                Map<Long, List<BaseCategoryView>> map3 = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory3Id));
                for (Map.Entry<Long, List<BaseCategoryView>> entry3 : map3.entrySet()) {
                    JSONObject jsonObject3 = new JSONObject();
                    jsonObject3.put("categoryId",entry3.getValue().get(0).getCategory3Id());
                    jsonObject3.put("categoryName",entry3.getValue().get(0).getCategory3Name());
                    jsonObject3List.add(jsonObject3);
                }
                jsonObject2.put("categoryChild",jsonObject3List);
            }
            jsonObject.put("categoryChild",jsonObject2List);
            return jsonObject;
        }
        return null;
    }

    @Override
    public List<BaseCategory1> findAllCategory1() {
        List<BaseCategory1> baseCategory1List = baseCategory1Mapper.selectList(new LambdaQueryWrapper<BaseCategory1>()
                .select(BaseCategory1::getId,BaseCategory1::getName));
        return baseCategory1List;
    }
}
